package com.zbkj.front.controller;

import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.CreateDisplaceRequest;
import com.zbkj.dal.pojo.response.ba.DisplaceDetailResponse;
import com.zbkj.dal.pojo.response.ba.DisplaceResponse;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.service.DisplaceService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 奢器置换
 * @author tianzhonghai
 * 2023/10/9 23:33
 */
@Slf4j
@RestController
@RequestMapping("api/front/she/displace")
@Api(tags = "奢器置换")
public class SheDisplaceController {
    @Autowired
    private DisplaceService displaceService;
    @Autowired
    private UserService userService;

    /**
     * 我的奢器置换记录
     * @param status
     * @param pageParamRequest
     * @return
     */
    @ApiOperation(value = "我的奢器置换记录")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<DisplaceResponse>> listMyDisplace(Integer status, PageParamRequest pageParamRequest) {
        return CommonResult.success(displaceService.listMyDisplace(status, pageParamRequest));
    }

    /**
     * 删除奢器置换记录
     * @param id
     */
    @ApiOperation(value = "删除奢器置换记录",notes = "删除奢器置换记录")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult<Boolean> deleteMyDisplace(@PathVariable("id")Integer id) {
        return CommonResult.success(displaceService.deleteMyDisplace(id));
    }

    /**
     * 提交奢器置换信息
     * @param createDisplaceRequest
     * @return
     */
    @ApiOperation(value = "提交奢器置换信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Boolean> create(@Validated @RequestBody CreateDisplaceRequest createDisplaceRequest) {
        return CommonResult.success(displaceService.create(createDisplaceRequest));
    }

    /**
     * 奢器置换记录详情
     * @param id
     * @return
     */
    @ApiOperation(value = "奢器置换详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<DisplaceDetailResponse> detail(@PathVariable("id") Integer id) {
        Integer userId = userService.getUserIdException();
        DisplaceDetailResponse displaceResponse = displaceService.detail(id);
        if (!ObjectUtils.nullSafeEquals(displaceResponse.getUid(), userId)){
            throw new CrmebException("您不能查看该信息");
        }
        return CommonResult.success(displaceResponse);
    }
}
